#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int cmp(const void* a, const void* b);
void my_sort(int arr[], int len);
int find_num(int arr[], int num, int len);
void print(int arr[] ,int len);

int main()
{
	int arr[100] = { 0 };
	srand((unsigned int)time(NULL));

	for (int i = 0; i < 100; i++)
	{
		arr[i] = rand() % 666;
	}

	int len = sizeof(arr) / sizeof(arr[0]);

	my_sort(arr, len);
	qsort(arr, 100, 4, cmp);

	int num = 0;
	scanf("%d", &num);
	int ret = find_num(arr, num, len);
	if (ret == -1)
	{
		printf("NO find!\n");
	}
	else
	{
		printf("num : -> %d\n", ret);
	}

	print(arr, len);
	return 0;
}

int cmp(const void* a, const void* b)
{
	return *(int*)a - *(int*)b;
}

void my_sort(int arr[], int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - 1 - i; j++)
		{
			int temp = 0;
			if (arr[j] > arr[j + 1])
			{
				temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}

int find_num(int arr[], int num, int len)
{
	int mid = 0;
	int flag = 1;
	int left = 0;
	int right = len - 1;

	while (left <= right)
	{
		int mid = left + (right - left) / 2;
		if (arr[mid] > num)
		{
			right = mid - 1;
		}
		else if (arr[mid] < num)
		{
			left = mid + 1;
		}
		else
		{
			flag = 0;
			break;
		}
	}
	if (flag == 0)
	{
		return mid;
	}

	return -1;
}

void print(int arr[], int len)
{
	for (int i = 0; i < len; i++)
	{
		printf("%3d ", arr[i]);
		if ((i + 1) % 6 == 0)
		{
			printf("\n");
		}
	}
}
